package com.yanglei.util;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * login type=1 
 * server to client userlist type=4
 * client (to server) message type 2 
 * server (to everyone) message type 3
 * close server window type=6
 * close client window type=5
 * close client window confirmation: 7
 * login result: 8
 * client single (to server) type 9
 * single request type=10
 * single response type=11
 * remove client type=12
 * single client close type =13
 * same name single type 14
 * */

public class XMLUtil {

	private static Document constructDocument() {
		Document document = DocumentHelper.createDocument();
		Element root = DocumentHelper.createElement("message");

		document.setRootElement(root);

		return document;
	}

	/**
	 * 客户端登录时向服务器发送的xml数据
	 * */
	public static String constructLoginXML(String username) {
		Document document = constructDocument();

		Element root = document.getRootElement();

		Element type = root.addElement("type");
		type.setText("1");

		Element user = root.addElement("user");
		user.setText(username);

		return document.asXML();
	}

	/**
	 * 从客户端登录时所发送的xml数据中解析出用户名
	 */
	public static String extractUsername(String xml) {
		try {
			SAXReader saxReader = new SAXReader();

			Document document = saxReader.read(new StringReader(xml));

			Element user = document.getRootElement().element("user");

			return user.getText();
		} catch (Exception e) {
			// TODO: handle exception
		}

		return null;
	}

	/**
	 * 向客户端发送在线用户列表的xml数据
	 */

	public static String constructUserList(Set<String> users) {
		Document document = constructDocument();
		Element root = document.getRootElement();
		Element type = root.addElement("type");
		type.setText("4");

		for (String user : users) {
			Element e = root.addElement("user");
			e.setText(user);
		}

		return document.asXML();
	}
	/**
	 * 从服务器发来的xml数据中提取出用户列表
	 * */
	public static Vector<String> extractUserList(String xml)
	{
		Vector<String> vector=new Vector<String>();
		
		try {
			SAXReader saxreader=new SAXReader();
			
			Document document=saxreader.read(new StringReader(xml));
			
			for(Iterator iter=document.getRootElement().elementIterator("user");iter.hasNext();)
			{
				Element e=(Element)iter.next();
				
				vector.add(e.getText());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return vector;
	}
	
	/**
	 * 从xml中解析出type的值
	 * */
	public static String extractType(String xml)
	{
		try {
			SAXReader saxReader=new SAXReader();
			
			Document document=saxReader.read(new StringReader(xml));
			
			Element typeElement=document.getRootElement().element("type");
			
			return typeElement.getText();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	/**
	 * 客户端向服务器发送的聊天信息的xml数据
	 * @param username
	 * @param message
	 * @return
	 */
	public static String constructMessageXML(String username,String message)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("2");
		
		Element user=root.addElement("user");
		user.setText(username);
		
		Element content=root.addElement("content");
		content.setText(message);
		
		return document.asXML();
	}
	
	/**
	 * 从客户端向服务器端发送的xml聊天数据解析出聊天内容
	 * */
	public static String extracContent(String xml)
	{
		try {
			SAXReader saxReader=new SAXReader();
			Document document=saxReader.read(new StringReader(xml));
			Element contentElement=document.getRootElement().element("content");
			
			return contentElement.getText();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	/**
	 * 构造服务器向所有客户端发送的xml聊天数据
	 * */
	
	public static String constructServerMessageXML(String message)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("3");
		
		Element content=root.addElement("content");
		content.setText(message);
		
		return document.asXML();
	}
	
	/**
	 * 构造服务器端窗口关闭的xml数据
	 * */
	
	public static String construcCloseServerWindowXML()
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("6");
		
		return document.asXML();
	}
	/**
	 * 构造客户端窗口关闭的xml数据
	 * */
	
	public static String constructCloseClientWindowXML(String username)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("5");
		
		Element user=root.addElement("user");
		user.setText(username);
		
		return document.asXML();
	}
	
	/**
	 * 构造服务器端确认客户端关闭的xml信息
	 * */
	
	public static String constructCloseClientWindowConfirmationXML()
	{
		Document document = constructDocument();
		Element root = document.getRootElement();
		
		Element type = root.addElement("type");
		type.setText("7");
		
		return document.asXML();
	}
	
	/**
	 * 构造向客户端返回的登录结果XML
	 */
	public static String constructLoginResultXML(String result)
	{
		Document document = constructDocument();
		Element root = document.getRootElement();
		
		Element typeElement = root.addElement("type");
		typeElement.setText("8");
		
		Element resultElement = root.addElement("result");
		resultElement.setText(result);
		
		return document.asXML();
	}
	
	/**
	 * 从xml数据中解析出登录结果
	 */
	
	public static String extractLoginResult(String xml)
	{
		String result = null;
		
		try
		{
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(xml));
			
			Element root = document.getRootElement();
			
			result = root.element("result").getText();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return result;
	}
	
	/**
	 * 构造客户端到服务器的私聊xml数据
	 * */
	public static String constructSingleClientServer(String message,String user,String user2)
	{
		Document document = constructDocument();
		Element root = document.getRootElement();
		
		Element typeElement = root.addElement("type");
		typeElement.setText("9");
		
		Element nameElement1 = root.addElement("user");
		nameElement1.setText(user);
		
		Element nameElement2 = root.addElement("user2");
		nameElement2.setText(user2);
		
		Element content=root.addElement("content");
		content.setText(message);
		
		return document.asXML();
	}
	
	/**
	 * 从客户端发来的私聊xml数据中提取user2 
	 * */
	
	public static String extractUsername2(String xml) {
		try {
			SAXReader saxReader = new SAXReader();

			Document document = saxReader.read(new StringReader(xml));

			Element user = document.getRootElement().element("user2");

			return user.getText();
		} catch (Exception e) {
			// TODO: handle exception
		}

		return null;
	}
	/**
	 * 构造私聊请求的xml数据
	 * */
	public static String construcSingleRequestXML(String username,String username2)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("10");
		
		Element nameElement1 = root.addElement("user");
		nameElement1.setText(username);
		
		Element nameElement2 = root.addElement("user2");
		nameElement2.setText(username2);
		
		return document.asXML();
	}
	/**
	 * 构造私聊请求返回的xml数据
	 * */
	public static String construcSingleResponseXML(String message,String username,String username2)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("11");
		
		Element nameElement1 = root.addElement("user");
		nameElement1.setText(username);
		
		Element nameElement2 = root.addElement("user2");
		nameElement2.setText(username2);
		
		Element content=root.addElement("content");
		content.setText(message);
		
		return document.asXML();
	}
	/**
	 * 构造踢出用户的xml数据
	 * */
	public static String construcRemoveClientXML()
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("12");
		
		return document.asXML();
	}
	/**
	 * 构造服务器向所有客户端发送的私聊xml聊天数据
	 * */
	
	public static String constructSingleMessageXML(String message,String username)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("9");
		
		Element content=root.addElement("content");
		content.setText(message);
		
		Element user=root.addElement("user");
		user.setText(username);
		
		return document.asXML();
	}
	
	/**
	 * 构造私聊客户端窗口关闭的xml数据
	 * */
	
	public static String constructCloseSingleClientWindowXML(String username,String username2)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("13");
		
		Element user=root.addElement("user");
		user.setText(username);
		
		Element nameElement2 = root.addElement("user2");
		nameElement2.setText(username2);
		
		return document.asXML();
	}
	/**
	 * 构造自己不能与自己私聊的的xml数据
	 * */
	
	public static String constructSameNameSingleXML(String username)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("14");
		
		return document.asXML();
	}
	/**
	 * 构造文件传输请求的xml数据
	 * */
	public static String construcFileRequestXML(String username,String username2,String message)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("15");
		
		Element nameElement1 = root.addElement("user");
		nameElement1.setText(username);
		
		Element nameElement2 = root.addElement("user2");
		nameElement2.setText(username2);
		
		Element content=root.addElement("content");
		content.setText(message);
		
		return document.asXML();
	}
	/**
	 * 构造文件传输请求返回的xml数据
	 * */
	public static String construcSendFileResponseXML(String message,String username,String username2,String port,String address)
	{
		Document document=constructDocument();
		Element root=document.getRootElement();
		
		Element type=root.addElement("type");
		type.setText("16");
		
		Element nameElement1 = root.addElement("user");
		nameElement1.setText(username);
		
		Element nameElement2 = root.addElement("user2");
		nameElement2.setText(username2);
		
		Element content=root.addElement("content");
		content.setText(message);
		
		Element port1=root.addElement("port");
		port1.setText(port);
		
		Element address1=root.addElement("address");
		address1.setText(address);
		
		return document.asXML();
	}
	/**
	 * 从客户端发来的文件传输xml数据中提取port 
	 * */
	
	public static String extractPort(String xml) {
		try {
			SAXReader saxReader = new SAXReader();

			Document document = saxReader.read(new StringReader(xml));

			Element port = document.getRootElement().element("port");

			return port.getText();
		} catch (Exception e) {
			// TODO: handle exception
		}

		return null;
	}
	/**
	 * 从客户端发来的文件传输xml数据中提取address 
	 * */
	
	public static String extractAddress(String xml) {
		try {
			SAXReader saxReader = new SAXReader();

			Document document = saxReader.read(new StringReader(xml));

			Element address = document.getRootElement().element("address");

			return address.getText();
		} catch (Exception e) {
			// TODO: handle exception
		}

		return null;
	}
}
